package com.fusetech.web.flow.controller.audit;


import com.fusetech.common.core.controller.BaseController;
import com.fusetech.common.core.domain.entity.SysRole;
import com.fusetech.common.core.domain.entity.SysUser;
import com.fusetech.common.exception.ServiceException;
import com.fusetech.flow.audit.apiService.IAuditApiService;
import com.fusetech.flow.audit.model.Audit;
import com.fusetech.flow.audit.model.AuditResponse;
import com.fusetech.flow.audit.service.CurrentAuditNodeService;
import com.fusetech.flow.audit.service.dto.CurrentAuditNodeDTO;
import com.fusetech.flow.audit.supportService.IAuditChcekService;
import com.fusetech.flow.audit.util.AuditUtil;
import com.fusetech.framework.web.service.SysPermissionService;
import com.fusetech.os.core.response.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags ="审核流程API")
@RestController
@RequestMapping(value = "/erp/audit/auditApi")
public class AuditApiController extends BaseController {

    /*@Autowired
    private SysUserWrapper sysUserWrapperImpl;
    @Autowired
    private ErpManufacturerInformationService erpManufacturerInformationService;
    @Autowired
    private ErpSupplierProductionService erpSupplierProductionService;
    @Autowired
    private ErpCustomersService erpCustomersService;*/
    @Autowired
    private IAuditApiService iAuditApiService;
    @Autowired
    private IAuditChcekService iAuditChcekService;

    @Autowired
    private CurrentAuditNodeService currentAuditNodeService;
    @Autowired
    private SysPermissionService sysPermissionService;
    /*@Autowired
    private ICommodityApprovalService iCommodityApprovalService;
    @Autowired
    private IPurchaseOrderApprovalService iPurchaseOrderApprovalService;
    @Autowired
    private ErpSalesAuditHandle erpSalesAuditHandle;*/
    

   /* @ApiOperation(value = "检查当前用户是否有当前节点审核权限")
    //@PreAuthorize({"erp:audit:auditProcess:cheakAudit"})
    @GetMapping("cheakAudit")
    public ResponseEntity<?> cheakAudit(String id)  {
        List<SysRoleDTO> currentRole = SecurityUtils.getCurrentRole();
        SysUser sysUser = getLoginUser().getUser();
        //permissionService.
        CheckResponse checkResponse= iAuditChcekService.checkNodeUserRole(currentRole, sysUser, id);
        if (checkResponse.isFlag()){
            CurrentNode currentNode = iAuditApiService.getCurrentNode(id);
            HashMap map = new HashMap();
            map.put("role",checkResponse.isFlag());
            map.put("currentNode",currentNode);
            return ResponseMessage.OK(map);
        }else{
            return ResponseMessage.ERROR(checkResponse.getErrMsg());
        }
    }*/

    /*@ApiOperation(value = "检查当前用户是否有该类型流程审核权限")
    //@PreAuthorize({"erp:audit:auditProcess:cheakProcess"})
    @GetMapping("cheakProcess")
    public ResponseEntity<?> cheakProcess(String documentType) throws NoLoginException, AuthenicException {
        List<SysRoleDTO> currentRole = SecurityUtils.getCurrentRole();
        //SysUser sysUser = sysUserWrapperImpl.toEntity(SecurityUtils.getCurrentUser().getUser());
        CheckResponse checkResponse= iAuditChcekService.checkProcessUserRoleResponse(currentRole,documentType);
        if (checkResponse.isFlag()){
            HashMap map = new HashMap();
            map.put("role",checkResponse.isFlag());
            return ResponseMessage.OK(map);
        }else{
            return ResponseMessage.ERROR(checkResponse.getErrMsg());
        }
    }*/

    @ApiOperation(value = "审核流程-审批任务")
    //@PreAuthorize({"erp:audit:auditProcess:auditInfoProcess"})
    @GetMapping("auditInfoProcess")
    public ResponseEntity<?> auditInfoProcess(String ids,String status,String idea) throws  ServiceException {
        String[] split = ids.split(",");
        SysUser sysUser = getLoginUser().getUser();
        List<SysRole> currentRoleList = sysPermissionService.getRolePermissionList(sysUser);
        /*SysUser sysUser = sysUserWrapperImpl.toEntity(SecurityUtils.getCurrentUser().getUser());
        List<SysRoleDTO> currentRole =SecurityUtils.getCurrentRole();*/
        for (String id : split) {

            CurrentAuditNodeDTO currentAuditNodeDTO = currentAuditNodeService.findById(id);
            Audit audit = AuditUtil.buildAuditParam(currentAuditNodeDTO, sysUser, status, idea,currentRoleList);
            AuditResponse auditResponse = iAuditApiService.auditApi(audit);
            //1 生产厂商  2 供应商   3 客户
            if (auditResponse != null && auditResponse.getCode().equals("200")) {
                /*if (currentAuditNodeDTO.getDocumentType().equals("1")) {
                    erpManufacturerInformationService.saveFactoryAprrvoe(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                } else if (currentAuditNodeDTO.getDocumentType().equals("2")) {
                    erpSupplierProductionService.saveSupplierApprove(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                } else if (currentAuditNodeDTO.getDocumentType().equals("3")) {
                    erpCustomersService.saveCustomerApprove(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                }else if (currentAuditNodeDTO.getDocumentType().equals("4")||currentAuditNodeDTO.getDocumentType().equals("5")||currentAuditNodeDTO.getDocumentType().equals("6")||currentAuditNodeDTO.getDocumentType().equals("7")) {
                    iCommodityApprovalService.saveCommodityApproval(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                }else if(currentAuditNodeDTO.getDocumentType().equals("8")){
                    iPurchaseOrderApprovalService.savePurchaseOrderApproval(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                }else if(currentAuditNodeDTO.getDocumentType().equals("9")){
                    erpSalesAuditHandle.auditCallbackApproval(currentAuditNodeDTO,auditResponse,idea,status,SecurityUtils.getCurrentUserId());
                }*/
                return ResponseMessage.OK("审核成功");
            } else {
                return ResponseMessage.ERROR(auditResponse.getMsg());
            }
        }
        return ResponseMessage.OK("审核成功");
    }


}
